package com.classbook.violet.baigiang6.fragment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.classbook.violet.baigiang6.activity.MainActivity;
import com.classbook.violet.baigiang6.adapter.ListLessonAdapter;
import com.classbook.violet.baigiang6.adapter.ListUnitAdapter;
import com.classbook.violet.baigiang6.database.DatabaseHelper;
import com.classbook.violet.baigiang6.model.ConnectionDetector;
import com.classbook.violet.baigiang6.subject.LessonNode;
import com.classbook.violet.baigiang6.subject.SubjectParser;
import com.classbook.violet.baigiang6.subject.UnitNode;
import com.classbook.violet.baigiang7.R;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.Config;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class SubjectFragment extends Fragment{
	private ListView lv_book;
	private ListView lv_unit;
	private  String SUBJECT_NAME;
	private  String FILE_LINK;
	private String PATH = Environment.getExternalStorageDirectory().getPath() + "/EDC/Violet/";
	private String URL = "http://classbook.vn/cbs/violet/download";
	
	private ListLessonAdapter listLessonAdapter;
	private ListUnitAdapter listUnitAdapter;
	List<LessonNode> lessonList;
	List<UnitNode> unitList;
	SubjectParser mParser;
	public Boolean isLoadCover = true;
	public ConnectionDetector mConnect;
	DatabaseHelper db;
	
	private File mFolder;
	
	private Bundle mBundle;
	public SubjectFragment(){}
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
//		//Log.e("HoaHocFragment", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
		mBundle = getArguments();
		SUBJECT_NAME = mBundle.getString("name");
		FILE_LINK = mBundle.getString("link");
//		Log.i(SUBJECT_NAME, FILE_LINK);
		
		mFolder = new File(PATH + FILE_LINK);
		if (!mFolder.exists()){
			mFolder.mkdirs();
		}
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
	
		View view = inflater.inflate(R.layout.monhoc, null);
//		Log.v(SUBJECT_NAME, FILE_LINK);
		lv_book = (ListView) view.findViewById(R.id.lv_lesson);
		lv_unit = (ListView) view.findViewById(R.id.lv_unit);
		
		return view;
	}
	
	public void setPath(List<LessonNode> list){
		for (int i = 0; i < list.size(); i++){
			list.get(i).path = FILE_LINK + list.get(i).url;
		}
	}
	public void checkLike (List<LessonNode> lessonList){
		for (int i = 0; i < lessonList.size(); i++){
			lessonList.get(i).isLike = db.checkLessonCategory("like", lessonList.get(i));
		}
	}
	
	public void checkThumb (List<LessonNode> lessonList){
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inSampleSize = 4;
		for (int i = 0; i < lessonList.size(); i++){
			File thumb = new File(PATH + FILE_LINK + lessonList.get(i).url + ".jpg");
			if (thumb.exists() && lessonList.get(i).cover == null){
				Bitmap bm =  BitmapFactory.decodeFile(thumb.getAbsolutePath()) ;
				lessonList.get(i).cover = Bitmap.createScaledBitmap(bm, 160, 120, true);
				bm.recycle();
			}
		}
	}
	
	public List<LessonNode> CutList (List<LessonNode> subjectList, String key){
		List<LessonNode> subjectTemp = new ArrayList<LessonNode>();
		for(int i = 0; i < subjectList.size(); i++){
			if (subjectList.get(i).url.contains(key)){
				subjectTemp.add(subjectList.get(i));
			}
		}
		return subjectTemp;
	}


	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.d("ONPAUSE", SUBJECT_NAME);
//		clearBitmap(lessonList);
	}


	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.d("ONRESUME", SUBJECT_NAME);
		db = new DatabaseHelper(getActivity());
		lessonList = new ArrayList<LessonNode>();
		unitList = new ArrayList<UnitNode>();
		mConnect = new ConnectionDetector(getActivity());
		try {
			
			mParser = ((MainActivity)getActivity()).getParser();
			if (mParser.isParser){
				Log.v("PARSER SUBJECT", SUBJECT_NAME);
	        	lessonList = mParser.parseLesson(SUBJECT_NAME);
	        	checkThumb(lessonList);
	        	checkLike(lessonList);
	        	setPath(lessonList);
	        	unitList = mParser.parseUnit(SUBJECT_NAME);
			}
        	unitList.add(0, new UnitNode("Tất cả bài giảng", lessonList));
        	listLessonAdapter = new ListLessonAdapter(getActivity(), lessonList, "sub-" + FILE_LINK);
        	listUnitAdapter = new ListUnitAdapter(getActivity(), unitList);
        	listUnitAdapter.getItem(0).ischeck = true;
		}catch (Exception e){
			Log.e("PARSER XML", e.toString());
		}
		lv_book.setAdapter(listLessonAdapter);
		lv_unit.setAdapter(listUnitAdapter);
		lv_unit.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				listUnitAdapter.disableCheck();
				listUnitAdapter.getItem(arg2).ischeck = true;
				listUnitAdapter.notifyDataSetChanged();
				if (arg2 != 0){
					isLoadCover = false;
				}else{
					isLoadCover = true;
				}
				listLessonAdapter = new ListLessonAdapter(getActivity(), listUnitAdapter.getItem(arg2).lessonList, "sub-" + FILE_LINK);
				lv_book.setAdapter(listLessonAdapter);
				listLessonAdapter.notifyDataSetChanged();
				
			}
		});
	}
	
	
}
